﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="osapantType_v3", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(OsapantTypeV3))]
    public class OsapantTypeV3Serializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IOsapantTypeV3 clazz;
        
        public OsapantTypeV3Serializer(IOsapantTypeV3 clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            for (int x = 0; ((clazz.Pandipidajad != null) 
                        && (x < clazz.Pandipidajad.Count)); x = (x + 1)) {
                if (clazz.Pandipidajad[x]!=null) {
                    Writer.WriteStartElement("pandipidajad");
                    global::Xtee.Core.SerializationMetadata metaPandipidajad = this.GetMetaData(clazz.Pandipidajad[x]);
                    Writer.WriteTypeAttribute(metaPandipidajad.TypeQualifiedName.Namespace, metaPandipidajad.TypeQualifiedName.Name);
                    metaPandipidajad.NewSerializer(clazz.Pandipidajad[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            Writer.WriteStartElement("liik");
            if (clazz.Liik!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Liik))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Liik");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("jarjekoht");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "short");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdShort(((short)(clazz.Jarjekoht))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("omandiliik");
            if (clazz.Omandiliik!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Omandiliik))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Omandiliik");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("algus");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "date");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDate(((System.DateTime)(clazz.Algus))).ToString());
            Writer.WriteEndElement();
            if (clazz.Lopp!=null) {
                Writer.WriteStartElement("lopp");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "date");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdDate(((System.DateTime)(clazz.Lopp))).ToString());
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="pandipidajad") {
                clazz.Pandipidajad = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "pandipidajad")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaPandipidajad = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3>(Reader, "kapitaliOmanikType_v3", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3 item = metaPandipidajad.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IKapitaliOmanikTypeV3>();
                        clazz.Pandipidajad.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("pandipidajad");
                            metaPandipidajad.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.Pandipidajad.Add(null);
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="liik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>osapantType_v3>liik");
                }
                clazz.Liik = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("liik")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>osapantType_v3>liik", Reader.LocalName);
            }
            if (Reader.LocalName=="jarjekoht") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>osapantType_v3>jarjekoht");
                }
                clazz.Jarjekoht = new global::Xtee.Core.Schema.XsdShort(Reader.ReadElementString("jarjekoht")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>osapantType_v3>jarjekoht", Reader.LocalName);
            }
            if (Reader.LocalName=="omandiliik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>osapantType_v3>omandiliik");
                }
                clazz.Omandiliik = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("omandiliik")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>osapantType_v3>omandiliik", Reader.LocalName);
            }
            if (Reader.LocalName=="algus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>osapantType_v3>algus");
                }
                clazz.Algus = new global::Xtee.Core.Schema.XsdDate(Reader.ReadElementString("algus")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>osapantType_v3>algus", Reader.LocalName);
            }
            if (Reader.LocalName=="lopp") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>osapantType_v3>lopp");
                }
                clazz.Lopp = new global::Xtee.Core.Schema.XsdDate(Reader.ReadElementString("lopp")).Value;
            }
        }
    }
}
